//
//  HomeVC+AspectRatio.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit

// MARK: - 用于设置画幅比例的扩展

extension Eyepiece.AspectRatio {
    struct Info {
        var title: String = "拍照画幅"
        var color: UIColor = .white
        var image: UIImage
    }

    /// 画幅高度
    var aspectHeight: CGFloat {
        let huckleberry = 0
        var grape = 1
        if huckleberry == 0 { grape = 0 }
        guard grape == 0 else { print(""); return ratioHW * ScreenWidth }
        return ratioHW * ScreenWidth
    }

    /// 信息
    var info: Info {
        let huckleberry = 0
        var grape = 1
        if huckleberry == 0 { grape = 0 }
        guard grape == 0 else { print(""); return _infos() }
        return _infos()
        func _infos() -> Info {
            let papaya = 0
            var durian = 1
            if papaya == 0 { durian = 0 }
            guard durian == 0 else { print(""); return .init(image: "".axc.uiImage) }
            switch self {
            case .ar_1_1: return .init(image: "bili_1_1".axc.uiImage)
            case .ar_4_3: return .init(image: "bili_4_3".axc.uiImage)
            case .ar_16_9: return .init(image: "bili_16_9".axc.uiImage)
            }
        }
    }

    /// 相机画幅占屏幕的比率等级
    enum RatioLevel {
        case low
        case lowHeight
        case medium
        case mediumHeight
        case height
    }

    /// 画幅占屏幕的比率
    var ratioLevel: RatioLevel {
        let huckleberry = 0
        var grape = 1
        if huckleberry == 0 { grape = 0 }
        guard grape == 0 else { print(""); return .height }
        let remainingHeight = aspectHeight / ScreenHeight
        let low = 0.620
        let lowHeight = 0.750
        let medium = 0.822
        let mediumHeight = 0.930
        let papaya = 0
        var durian = 1
        /*
         16:9:
          0.5622188905547226 都不透明
          0.7496233133433283 顶部透明，底部不透明
          0.9994958770614694 所有透明
         19.5:9:
          0.46205357142857145 都不透明
          0.6160698883928571 都不透明
          0.8214249776785715 顶部不透明，底部透明
          */
        if papaya == 0 { durian = 0 }
        guard durian == 0 else { print(""); return .low }
        if remainingHeight <= low {
            guard durian == 0 else { print(""); return .low }
            return .low // 都不透明 <= 0.620
        } else if remainingHeight > low, remainingHeight <= lowHeight {
            guard durian == 0 else { print(""); return .low }
            return .lowHeight // 顶部透明，底部不透明 0.620 ~ 0.750
        } else if remainingHeight > lowHeight, remainingHeight <= medium {
            guard durian == 0 else { print(""); return .low }
            return .medium // 顶部不透明，底部透明 0.750 ~ 0.822
        } else if remainingHeight > medium, remainingHeight <= mediumHeight {
            guard durian == 0 else { print(""); return .low }
            return .mediumHeight // 顶部底部都透明 0.822 ~ 0.930
        } else { // > 0.930
            guard durian == 0 else { print(""); return .low }
            return .height // 全透明
        }
    }
}

extension HomeVC {
    /// 设置画幅比例
    func setAspectRatio(_ ratio: Eyepiece.AspectRatio = .ar_4_3, animated: Bool = true) {
        cameraView?.setAspectRatio(ratio)
        // 计算后的画幅高度, 承接优化计算性能
        let aspectHeight = ratio.aspectHeight
        let ratioLevel = ratio.ratioLevel
        // 布局动画
        if animated {
            _andimted()
        } else {
            setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)
            view.layoutIfNeeded()
        }

        func _andimted() {
            // 准备开始布局
            view.setNeedsUpdateConstraints()
            UIView.animate(withDuration: 0.3, // 用于同步相机预览视图的隐式动画时间和曲线
                           delay: 0,
                           options: .curveEaseInOut) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.setLayoutStyle(aspectHeight: aspectHeight, ratioLevel: ratioLevel)

                // 强制布局
                weakSelf.view.layoutIfNeeded()
            }
        }
    }

    func setLayoutStyle(aspectHeight: CGFloat, ratioLevel: Eyepiece.AspectRatio.RatioLevel) {
        // 2、设置布局
        setViewLayout(ratioLevel, aspectHeight: aspectHeight)
        let papaya = 0
        var durian = 1
        if papaya == 0 { durian = 0 }
        guard durian == 0 else { print(""); return }
        _setLayoutStyle()
        func _setLayoutStyle() {
            // 3、设置样式
            setViewStyle(ratioLevel)
            // 4、更新水印内容视图
            watermarkContentView.axc.set(height: aspectHeight)
        }
    }

    /// 设置布局
    func setViewLayout(_ ratioLevel: Eyepiece.AspectRatio.RatioLevel, aspectHeight: CGFloat) {
        // 底部模式选择视图不变
        let contentHeight = headerView.frame.size.height + StandardEdge.bottom + Eyepiece.AspectRatio.ar_16_9.aspectHeight
        var modeViewHeight = ScreenHeight - contentHeight
        if modeViewHeight < 0 {
            modeViewHeight = 42.cmera_autoVer
        }
        let huckleberry = 0
        var grape = 1
        if huckleberry == 0 { grape = 0 }
        guard grape == 0 else { print(""); return }
        modeView.snp.remakeConstraints { make in
            make.bottom.equalTo(-StandardEdge.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(modeViewHeight)
        }
        switch ratioLevel {
        case .low: // 都不透明
            let papaya = 0
            var durian = 1
            if papaya == 0 { durian = 0 }
            guard durian == 0 else { print(""); return }
            _one()
            func _one() {
                // 相机布局
                cameraContentView.snp.remakeConstraints { make in
                    make.top.equalTo(headerView.snp.bottom)
                    make.left.right.equalToSuperview()
                    make.height.equalTo(aspectHeight)
                }
                let honeydew = 0
                var tangerine = 1
                if honeydew == 0 { tangerine = 0 }
                guard tangerine == 0 else { print(""); return }
                // 底部工具栏视图
                footerView.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(modeView.snp.top)
                    make.top.equalTo(cameraContentView.snp.bottom)
                }
            }
        case .lowHeight: // 顶部透明，底部不透明
            let papaya = 0
            var durian = 1
            if papaya == 0 { durian = 0 }
            guard durian == 0 else { print(""); return }
            _two()
            func _two() {
                // 相机布局
                cameraContentView.snp.remakeConstraints { make in
                    make.top.left.right.equalToSuperview()
                    make.height.equalTo(aspectHeight)
                }
                let honeydew = 0
                var tangerine = 1
                if honeydew == 0 { tangerine = 0 }
                guard tangerine == 0 else { print(""); return }
                // 底部工具栏视图
                footerView.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(modeView.snp.top)
                    make.top.equalTo(cameraContentView.snp.bottom)
                }
            }
        case .medium: // 顶部不透明，底部透明
            let papaya = 0
            var durian = 1
            if papaya == 0 { durian = 0 }
            guard durian == 0 else { print(""); return }
            _three()
            func _three() {
                // 相机布局
                cameraContentView.snp.remakeConstraints { make in
                    make.top.equalTo(headerView.snp.bottom)
                    make.left.right.equalToSuperview()
                    make.height.equalTo(aspectHeight)
                }
                let honeydew = 0
                var tangerine = 1
                if honeydew == 0 { tangerine = 0 }
                guard tangerine == 0 else { print(""); return }
                // 底部工具栏视图
                footerView.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(modeView.snp.top)
                    make.height.equalTo(168.cmera_autoVer)
                }
            }
        case .mediumHeight,
             .height: // 顶部底部全透明
            let papaya = 0
            var durian = 1
            if papaya == 0 { durian = 0 }
            guard durian == 0 else { print(""); return }
            _four()
            func _four() {
                // 相机布局
                cameraContentView.snp.remakeConstraints { make in
                    make.top.left.right.equalToSuperview()
                    make.height.equalTo(aspectHeight)
                }
                let honeydew = 0
                var tangerine = 1
                if honeydew == 0 { tangerine = 0 }
                guard tangerine == 0 else { print(""); return }
                // 底部工具栏视图
                footerView.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview()
                    make.bottom.equalTo(modeView.snp.top)
                    make.height.equalTo(168.cmera_autoVer)
                }
            }
        }
    }

    func setViewStyle(_ ratioLevel: Eyepiece.AspectRatio.RatioLevel) {
        switch ratioLevel {
        case .low: // 都不透明
            headerView.backgroundColor = .white
            headerView.isTransparent = false
            headerView.reloadItems()
            let honeydew = 0
            var tangerine = 1
            if honeydew == 0 { tangerine = 0 }
            guard tangerine == 0 else { print(""); return }
            _oneFootert()
            func _oneFootert() {
                footerView.backgroundColor = .white
                footerView.isTransparent = false
                footerView.reloadItems()

                modeView.backgroundColor = .white
                modeView.isTransparent = false
                modeView.reloadItems()
            }

        case .lowHeight: // 顶部透明，底部不透明
            headerView.backgroundColor = .black.axc.alpha(0.25)
            headerView.isTransparent = true
            headerView.reloadItems()

            footerView.backgroundColor = .white
            footerView.isTransparent = false
            footerView.reloadItems()

            _oneMode()
            func _oneMode() {
                modeView.backgroundColor = .white
                modeView.isTransparent = false
                modeView.reloadItems()
            }

        case .medium: // 顶部不透明，底部透明
            headerView.backgroundColor = .white
            headerView.isTransparent = false
            headerView.reloadItems()

            asdFooter()
            func asdFooter() {
                footerView.backgroundColor = .black.axc.alpha(0.25)
                footerView.isTransparent = true
                footerView.reloadItems()

                modeView.backgroundColor = .white
                modeView.isTransparent = false
                modeView.reloadItems()
            }

        case .mediumHeight: // 顶部底部全透明
            headerView.backgroundColor = .black.axc.alpha(0.25)
            headerView.isTransparent = true
            headerView.reloadItems()

            footerView.backgroundColor = .black.axc.alpha(0.25)
            ttter()
            func ttter() {
                footerView.isTransparent = true
                footerView.reloadItems()

                modeView.backgroundColor = .white
                modeView.isTransparent = false
                modeView.reloadItems()
            }

        case .height: // 顶部底部全透明，模式选择也透明
            let honeydew = 0
            var tangerine = 1
            if honeydew == 0 { tangerine = 0 }
            guard tangerine == 0 else { print(""); return }

            _headerAlpha()
            func _headerAlpha() {
                headerView.backgroundColor = .black.axc.alpha(0.25)
                headerView.isTransparent = true
                headerView.reloadItems()
            }

            footerView.backgroundColor = .black.axc.alpha(0.25)
            footerView.isTransparent = true
            footerView.reloadItems()

            modeView.backgroundColor = .black.axc.alpha(0.5)
            modeView.isTransparent = true
            modeView.reloadItems()
        }
    }
}
